﻿using System.Collections.Specialized;
using System.Diagnostics;
using System.Configuration;

namespace NMA.Infrastructure.Configuration
{
    using NMA.Infrastructure.DBC;

    public class ConfigurationManagerWrapper : IConfigurationManager
    {
        private string _providerName;
        private string _connectionString;

        public NameValueCollection AppSettings
        {
            [DebuggerStepThrough]
            get
            {
                return ConfigurationManager.AppSettings;
            }
        }

        /// <summary>
        /// Set ConnectionString name to Configuration Manager Wrapper
        /// Using Fluent Interface for registered Configuration Manager object
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [DebuggerStepThrough]
        public IConfigurationManager SetConnectionStringName(string name)
        {
            Check.Assert(string.IsNullOrEmpty(name) == false, "ConnectionString name is null or empty");

            ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings[name];
            _providerName = connectionStringSettings.ProviderName;
            _connectionString = connectionStringSettings.ConnectionString;

            return this;
        }

        public string ProviderName
        {
            [DebuggerStepThrough]
            get { return _providerName; }
        }

        public string ConnectionString
        {
            [DebuggerStepThrough]
            get { return _connectionString; }
        }

        [DebuggerStepThrough]
        public T GetSection<T>(string sectionName)
        {
            Check.Assert(string.IsNullOrEmpty(sectionName) == false, "SectionName [" + sectionName + "] is null or empty");

            return (T)ConfigurationManager.GetSection(sectionName);
        }
    }
}